Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1l  m  Compiler error at line 12.  Compiler error at line 13.  The following three rules are copied from the Java Language Specification. 1. Comments do not nest. 2. /* and */ have no special meaning in comments that begin with //. 3. // has no special meaning in comments that begin with /* or /**. The three rules have the following implications. Once a comment is started the compiler will ignore additional comment start markers such as those associated with comments 2, 6, 7, 8, and 9. However, the compiler will not accept repeated comment end markers such as those associated with comments 10, 11, and 12.  
2c  Compiler error at 3.  The compiler creates a constructor for class C implicitly. The implicitly created constructor accepts no parameters and has no throws clause. The constructor for class B and class C both invoke the constructor for A. The constructor for class A declares Exception in the throws clause. Since the constructors for B and C invoke the constructor for A implicitly, both B anc C must declare Exception in their throws clause. A compile time error is generated at marker 3 because the default constructor does not declare Exception in the throws clause.  
3f  Compile-time Error.  A compile-error is the result of replacing the commas with semicolons in the declaration of the array.  
4f  Compile-time Error.  A compile-error is the result of replacing the curly braces with square braces in the declaration of the arrays.  
5a  d  f  g  public  abstract  final  strictfp  The access modifiers, protected and private, can be applied to a member class but not to a local class or a top level class. The static modifier can be applied to a member class but not a local class or a top level class. The public modifier can be applied to a top level class to allow the class to be access from outside of the package. The abstract modifier prevents the class from being instantiated. An abstract class may include zero, one or more abstract methods. The final modifier prevents a class from being extended. The strictfp modifier can be applied to a class, interface, or method to guarantee that all float or double operations are processed according to the IEEE 754 standard. Without the strictfp modifier the float or double operations can be processed using the greatest precision available. For example, a 64 bit CPU could process intermediate float operations using 64 rather than only 32 bits. If a class is marked strictfp then all float or double expressions within all nested classes, methods, constructors, static initializers, instance initializers, and variable initializers will also be strictfp.  
6e  Prints: 2,5,9  Arrays a1, a2 and a3 are all arrays where each element is an array of primitives of type int. The size of each of the 3 primitive int arrays are different. The size of the first is 2, the second is 3, and the third is 4. The declarations of a1, a2, and a3 are different in terms of the position of the square brackets, but the result of each is the same.  
7a  e  1.  5.  An array creation expression must have either a dimension expression or an initializer. If both are present then a compile-error is generated. Similarly, if neither is present then a compile-error is generated. If only the dimension expression is present then an array with the specified dimension is created with all elements set to the default values. If only the initializer is present then an array will be created that has the required dimensions to accommodate the values specified in the initializer. Java avoids the possibility of an incompatible dimension expression and initializer by not allowing both to appear in the same array creation expression. A compile-time error is generated by the array creation expression for a1 because it needs either a dimension expression or an initializer. A compile-time error is generated at 5 because either the dimension expression or the initializer must be removed.  
8h  transient  A transient field is not part of the persistent state of an object and therefore is not serialized. Please see section 8.3.1.3 of the Java Language Specification for more information.  
9a  d  e  h  class A extends Object.  The compiler will insert a default constructor implicitly.  The implicit constructor declaration has no throws clause.  The implicitly declared constructor invokes the no-parameter constructor of the superclass.  Field i1 and m1 both have package access. When no constructor is declared explicitly the compiler will insert one explicitly. The implicitly declared default construct will have the same access privileges as the class. In this case, the class is public so the default constructor is also public. The defaul constructor accepts no parameters and throws no exceptions.  
10c  g  3  7  The abstract modifier can be applied to a class and a method but not a field. A field is sometimes shared between threads. The volatile modifier is used to force threads to reconcile their own working copy of a field with the master copy in main memory. If a field is declared final then its value does not change and there is no need for threads to reconcile their own working copies of the variable with the master copy in main memory.  
11a  1.  A compile-time error occurs at the line marked 1 because it is not possible to use the type name to specify the dimensions of the array.  
12d  Declare all constructors using the private access modifier.  If no constructors are declared explicitly then the compiler will create one implicitly and it will have the same access modifier as the class. The explicit declaration of any constructor will prevent the creation of a default constructor. If the explicit constructors are declared private then no code outside of the class will have access to the constructors and will not have the ability to create an instance of the class. Constructor declarations do not include a return type so the void type can not be specified.  
13f  Compile-time Error.  A compile-error is the result of replacing the curly braces with parenthesis in the declaration of the array.  
14a  b  c  The compiler will create a default constructor if no other constructor is declared.  The default constructor takes no parameters.  The default constructor invokes the superclass constructor with no arguments.  If no constructor is explicitly declared in a class, then the compiler will implicitly insert a default constructor. The default constructor takes no parameters and invokes the superclass constructor with no parameters. If the superclass does not have an accessible no parameter constructor then a compile time error is generated. The access modifier implicitly assigned to the default constructor is the same as that assigned to the class. The default construct does not have a throws clause. Consequently, a compile time error is generated if the no parameter constructor of the super class has a throws clause.  
15e  Prints: 2,3,5  Arrays a1, a2 and a3 are all arrays where each element is an array of primitives of type int. The size of each of the 3 primitive int arrays are different. The size of the first is 2, the second is 3, and the third is 4. The declarations of a1, a2, and a3 are different in terms of the position of the square brackets, but the result of each is the same.  
16c  Prints: 4  The array, a, is an array containing four arrays so the length is 4.  
17k  Run-time Exception  Line 3 creates a three dimensional array where each element is a null reference to an object of type A. Array a3 is actually a single dimensional array with three elements. Each of the three elements is a two dimensional array and the dimensions of each is 3X3. Line 7 replaces each 3X3 array with a 2X1 array, a2. Line eight attempts to access an element that is not available in the new 2X1 array.  
18b  Prints: 147258369  If array index i had been the first index of the array, a1, then the output would have been 123456789. Since i is the second index of the array the output is 147258369.  

Copyright © 2002, Dan Chisholm
All rights reserved.
1